package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.entity.NursingProjectPagePram;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description NursingProjectServicImpl
 * @Author Bafeii7
 * @Date 2025-05-08 10:49
 */
@Service
public class NursingProjectServiceImpl implements NursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    // 分页查询
    @Override
    public PageResponse<Object> listByPage(NursingProjectPagePram nursingProjectPagePram) {
        PageHelper.startPage(nursingProjectPagePram.getPageNum(), nursingProjectPagePram.getPageSize());
        Page<NursingProject> page = nursingProjectMapper.ListByPage(nursingProjectPagePram);
        PageResponse pageResponse = new PageResponse(page);
        pageResponse.setRecords(page.getResult());
        return pageResponse;
    }


    @Override
    public NursingProjectVo getProjectById(Long id) {
        NursingProject np = nursingProjectMapper.getProjectById(id);
        NursingProjectVo npVo = BeanUtil.toBean(np, NursingProjectVo.class);
        return npVo;
    }

    @Override
    public void updateOrAdd(NursingProjectDto npDto) {
        NursingProject np = BeanUtil.toBean(npDto, NursingProject.class);
        if (np.getId() == null){
            np.setCreateBy(1L);
            np.setUpdateBy(1L);
            np.setCreateTime(LocalDateTime.now());
            np.setUpdateTime(LocalDateTime.now());
            nursingProjectMapper.addProject(np);
        }
        if (countById(npDto.getId()) != 0) {
            throw new  BaseException(BasicEnum.NURSINGPROJECT_UPDATE_FAIL);
        }
            np.setUpdateBy(1L);
            np.setUpdateTime(LocalDateTime.now());
            nursingProjectMapper.updateProject(np);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        if (countById(id) != 0) {
            throw new  BaseException(BasicEnum.NURSINGPROJECT_UPDATESTATUS_FAIL);
        }
        nursingProjectMapper.updateStatus(id, status);
    }

    @Override
    public void delete(Long id) {
        if (countById(id) != 0) {
            throw new BaseException(BasicEnum.NURSINGPROJECT_DELETE_FAIL);
        }
        nursingProjectMapper.delete(id);
    }

    @Override
    public List<NursingProjectVo> listAll() {
        return nursingProjectMapper.listAll();
    }

    public Integer countById(Long id){
        return nursingProjectMapper.countById(id);
    }
}
